var http = require('http')
var wsServerCreator = require('./index')
var path = require('path')
var fs = require('fs')

var port  = 8080
var server = http.createServer((req, res) => {
	var filePath = path.join(__dirname, 'static.html');

	var stat = fs.statSync(filePath)

	res.writeHead(200, {
		'Content-Type': 'text/html, charset=utf-8',
		'Content-Length': stat.size
	});

	var readStream = fs.createReadStream(filePath);
	readStream.pipe(res)
}).listen(port)

wsServerCreator(server, '/ws')

// server.on('clientError', (err, socket) => {
//   console.error(err)
// });

process.on('uncaughtException', function(err) {
	//防止Error: read ECONNRESET中断退出
	console.error(err.stack);
});

console.log("http server started in port", port);